package com.zgjkhis.modules.system.rest;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.system.domain.CptStore;
import com.zgjkhis.modules.system.domain.vo.CptStoreVO;
import com.zgjkhis.modules.system.service.CptStoreService;
import com.zgjkhis.modules.util.CharUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @date 2018-11-23
 */
@Api(tags = "门店管理：合作门店")
@RestController
@RequestMapping("/store/cpt")
public class CptStoreController {

    private static final String ENTITY_NAME = "cptStore";
    private final CptStoreService cptStoreService;

    public CptStoreController(CptStoreService cptStoreService) {
        this.cptStoreService = cptStoreService;
    }


    @Log("新增合作门店")
    @ApiOperation("新增合作门店")
    @PostMapping(value = "/addCptStore")
    public ResponseEntity<Object> addCptStore(@Validated @RequestBody List<CptStore> resources) {
//        List<CptStore> list=new ArrayList<>();
//        for (CptStore cptStore:resources) {
//            if(!cptStore.getStoreId().equals(cptStore.getCptStoreId())) {
//                CptStore temp = cptStoreService.getOneBystoreId(cptStore);
//                if(CharUtil.isEmpty(temp)){
//                    list.add(temp);
//                }
//            }
//        }
        if (CharUtil.isEmpty(resources) || resources.size() == 0) {
            throw new ServiceException("请选择合作药房");
        }
        cptStoreService.remove(Wrappers.<CptStore>query().lambda().eq(CptStore::getStoreId, resources.get(0).getStoreId()).or().eq(CptStore::getCptStoreId, resources.get(0).getStoreId()));
        cptStoreService.saveBatch(resources);
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @Log("查看合作门店")
    @ApiOperation("查看合作门店")
    @GetMapping(value = "/getCptStore")
    public ResponseEntity<List<CptStoreVO>> getCptStore(String storeId) {
        if (CharUtil.isEmpty(storeId)) {
            throw new ServiceException("请选择药房id");
        }
        List<CptStoreVO> list = cptStoreService.getCptStore(storeId);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

}
